package com.example.employee.restfulapi.controller;


import com.example.employee.restfulapi.entity.Company;
import com.example.employee.restfulapi.repository.CompanyRepository;
import com.example.employee.restfulapi.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CompanyController {

    @Autowired
    CompanyRepository companyRepository;

    //在此处完成Company API
    @GetMapping("/companies")
    public List<Company> getAllCompanies(){
        return companyRepository.findAll();
    }

    @GetMapping("/companies/{id}")
    public Company getCompanyById(@PathVariable int id){
        return companyRepository.findOne((long) id);
    }

//    @GetMapping("/companies/{id}/employees")
//    public List<Employee> getEmployeesByCompany(@PathVariable int id){
//        return employeeRepository.findOne(id)
//    }
    @GetMapping(" /companies/page/{page}/pageSize/{pageSize} ")
    public Page<Company> getCompanyByPage(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        System.out.println("aaa");
        return companyRepository.findAll(new PageRequest(page,pageSize));
    }

    @PostMapping("/post/companies")
    public Company addCompany(Company company){
         return companyRepository.save(company);
    }

    @PutMapping("/put/companies")
    public Company updateCompany(Company company){
        company.setCompanyName(company.getCompanyName());
        company.setEmployeesNumber(company.getEmployeesNumber());
        company.setId(company.getId());
        return companyRepository.save(company);
    }
    @DeleteMapping("/delete/companies/{id}")
    public void  deleteCompany(@PathVariable Long id){
        companyRepository.delete(id);
    }
}
